using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Text;
using Pegasus.Windows.Forms;

namespace RollManager.Library.Dialogs
{
    public partial class ChooseFolderDialog : FormEx
    {
        public string SelectedFolder
        {
            get
            {
                if( folderList.SelectedItem != null )
                {
                    return ( (DirectoryInfo) folderList.SelectedItem ).FullName;
                }

                return null;
            }
        }

        public ChooseFolderDialog( List<DirectoryInfo> dirs ) : base( "rollManagerConfig.chooseFolderDialog" )
        {
            InitializeComponent();

            folderList.DataSource = dirs;
            folderList.DisplayMember = "Name";
        }

        private void ChooseFolderDialog_FormClosing( object sender, FormClosingEventArgs e )
        {
            if( string.IsNullOrEmpty( SelectedFolder ) && DialogResult == DialogResult.OK )
            {
                e.Cancel = true;
            }
        }

        private void okButton_Click( object sender, EventArgs e )
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void folderList_DoubleClick( object sender, EventArgs e )
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void cancelButton_Click( object sender, EventArgs e )
        {
            Close();
        }

        private void folderList_SelectedValueChanged(object sender, EventArgs e)
        {
            if (folderList.SelectedItem != null)
            {
                DirectoryInfo di = (DirectoryInfo)folderList.SelectedItem;
                folderInfo.Text = GetDirectoryInfoAsString(di);
            }
            else
            {
                folderInfo.Text = "";
            }
        }

        private String GetDirectoryInfoAsString(
            DirectoryInfo di)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("CreationTime: {0}\r\n\r\n", di.CreationTime);
            sb.AppendFormat("LastWriteTime: {0}\r\n\r\n", di.LastWriteTime);
            sb.AppendFormat("FullName: {0}\r\n\r\n", di.FullName);
            return sb.ToString();
        }
    }
}